using System;
using CommandSystem;
using CustomCharacter.Ability;
using Exiled.API.Features;
using Utils.NonAllocLINQ;

namespace CustomCharacter.Commands.AbilityManagerCommands
{
    public class Revoke : ICommand
    {
        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ForceclassWithoutRestrictions))
            {
                response = "你没有权限使用这条命令！（需要权限：ForceclassWithoutRestrictions）";
                return false;
            }

            if (arguments.Count < 1)
            {
                response = "用法： abilitymanager(am) revoke 玩家ID <技能ID>";
                return false;
            }

            if (!int.TryParse(arguments.At(0), out var id))
            {
                response = "请输入一个有效的数字ID";
                return false;
            }

            if (!Player.TryGet(id, out var player))
            {
                response = "玩家不存在！";
                return false;
            }

            if (arguments.Count >= 2)
            {
                if (!AbilityManager.RegisteredAbilities.TryGetFirst(x => x.AbilityId == arguments.At(1),
                        out var ability))
                {
                    response = "未找到指定已注册的技能，请输入abilitymanager list查询已注册技能";
                    return false;
                }

                player.RevokeAbility(ability);
                response = "已夺去玩家指定技能！";
                return true;
            }

            player.RevokeAbility();
            response = "已移除玩家所有技能";
            return true;
        }

        public string Command { get; } = "revoke";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "删除玩家的某个技能或全部技能";
    }
}